#include<iostream>
#include<pthread.h>
#include<string>
#include<functional>
#include<unistd.h>
#include<sys/syscall.h>
#include<vector>

using func_t = std::function<void()>;

const int nums = 5;

#define get_lwp() syscall(SYS_gettid)

namespace THREAD
{
    
class Thread
{
public:
    Thread(const char* str,func_t fun)
        :_name(str)
        ,_fun(fun)
    {}

    Thread(std::string str,func_t fun)
        :_name(str)
        ,_fun(fun)
    {}

    static void* StartRoutine(void* args)
    {
        Thread* This = static_cast<Thread*>(args);
        This->_lwp = get_lwp();
        This->_fun();

        return (void*)0;
    }


    bool start()
    {
        int n = pthread_create(&_tid,nullptr,StartRoutine,this);
        if(n == 0)
            return true;
        else
            return false;
    }

    void join()
    {
        void* ret = nullptr;
        int n = pthread_join(_tid,&ret);
        if(n == 0)
        {
            std::cout << "回收新线程成功:" << _name << " 退出码: " << (long long int)ret << std::endl;
        }

        else
        {
            std::cout << "回收新线程失败" << std::endl;
        }



        
    }

    ~Thread()
    {}




private:
    pthread_t _tid;
    pid_t _lwp;
    std::string _name;
    func_t _fun;
};
}